package com.gx.smart.building.admin;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleContentReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleContentResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleModelQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleModelQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordDetailResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordListQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordListQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordOperateReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordPlusResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleTimeQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleTimeQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.ScheduleAppApiGrpc;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/9/8
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class ScheduleAppApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    /**
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordPlusResp> generateAppScheduleRecordQrCode(
            String id,
            CallBack<AppScheduleRecordPlusResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordPlusResp>(callBack) {
            @Override
            protected AppScheduleRecordPlusResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordReq message = AppScheduleRecordReq.newBuilder()
                        .setId(id)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AppScheduleRecordPlusResp response = null;
                try {
                    response = getStub(channel).generateAppScheduleRecordQrCode(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "generateAppScheduleRecordQrCode");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private ScheduleAppApiService() {
    }

    public static ScheduleAppApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private ScheduleAppApiGrpc.ScheduleAppApiBlockingStub getStub(ManagedChannel channel) {
        return ScheduleAppApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * APP查询交接记录
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordListQueryResp> queryAppScheduleRecordList(
            ComQuery comQuery,
            String userId,
            long beginTime,
            long endTime,
            CallBack<AppScheduleRecordListQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordListQueryResp>(callBack) {
            @Override
            protected AppScheduleRecordListQueryResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordListQueryReq message = AppScheduleRecordListQueryReq.newBuilder()
                        .setComQuery(comQuery)
                        .setUserId(userId)
                        .setTenantNo(1)
                        .setType(1)//类型 1查询我的所有 2查询我的交班记录 3查询我的接班记录 默认查所有
                        .setBeginTime(beginTime)
                        .setEndTime(endTime)
                        .build();
                Logger.d(message);
                AppScheduleRecordListQueryResp response = null;
                try {
                    response = getStub(channel).queryAppScheduleRecordList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAppScheduleRecordList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 发起交接——获取当前登录用户的交接页面
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleModelQueryResp> queryAppScheduleModel(
            String userId,
            CallBack<AppScheduleModelQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleModelQueryResp>(callBack) {
            @Override
            protected AppScheduleModelQueryResp doRequestData(ManagedChannel channel) {
                AppScheduleModelQueryReq message = AppScheduleModelQueryReq.newBuilder()
                        .setUserId(userId)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AppScheduleModelQueryResp response = null;
                try {
                    response = getStub(channel).queryAppScheduleModel(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAppScheduleModel");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 发起交接——查询该用户，对应班次下的可选择班时（返回对应班时和该班时在该交接班模板下的交接时间范围）
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleTimeQueryResp>
    queryAppScheduleTimeListByUser(
            String userId,
            String modelId,
            CallBack<AppScheduleTimeQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleTimeQueryResp>(callBack) {
            @Override
            protected AppScheduleTimeQueryResp doRequestData(ManagedChannel channel) {
                AppScheduleTimeQueryReq message = AppScheduleTimeQueryReq.newBuilder()
                        .setUserId(userId)
                        .setModelId(modelId)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AppScheduleTimeQueryResp response = null;
                try {
                    response = getStub(channel).queryAppScheduleTimeListByUser(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAppScheduleTimeListByUser");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 发起交接——获取读数
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleContentResp>
    queryAppScheduleContent(
            List<String> ids,
            CallBack<AppScheduleContentResp> callBack) {

        return new GrpcAsyncTask<String, Void, AppScheduleContentResp>(callBack) {
            @Override
            protected AppScheduleContentResp doRequestData(ManagedChannel channel) {
                AppScheduleContentReq message = AppScheduleContentReq
                        .newBuilder()
                        .addAllIds(ids)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AppScheduleContentResp response = null;
                try {
                    response = getStub(channel).queryAppScheduleContent(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAppScheduleContent");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 发起交接——断点记录——填写完未提交，返回了前一页之前的操作
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordResp> noteAppScheduleRecord(
            RecordDto content,
            CallBack<AppScheduleRecordResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordResp>(callBack) {
            @Override
            protected AppScheduleRecordResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordOperateReq message = AppScheduleRecordOperateReq
                        .newBuilder()
                        .setContent(content)
                        .build();
                Logger.d(message);
                AppScheduleRecordResp response = null;
                try {
                    response = getStub(channel).noteAppScheduleRecord(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "noteAppScheduleRecord");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 交接班——详情查看
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordDetailResp>
    queryAppScheduleRecordDetail(
            String id,
            CallBack<AppScheduleRecordDetailResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordDetailResp>(callBack) {
            @Override
            protected AppScheduleRecordDetailResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordReq message = AppScheduleRecordReq.newBuilder()
                        .setId(id)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AppScheduleRecordDetailResp response = null;
                try {
                    response = getStub(channel).queryAppScheduleRecordDetail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryAppScheduleRecordDetail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 交接班动作——交接单新增（返回成功与否，并附带一个二维码）
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordPlusResp>
    insertAppScheduleRecord(
            RecordDto content,
            CallBack<AppScheduleRecordPlusResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordPlusResp>(callBack) {
            @Override
            protected AppScheduleRecordPlusResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordOperateReq message = AppScheduleRecordOperateReq.newBuilder()
                        .setContent(content)
                        .build();
                Logger.d(message);
                AppScheduleRecordPlusResp response = null;
                try {
                    response = getStub(channel).insertAppScheduleRecord(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "insertAppScheduleRecord");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 交接班动作——交接单修改（返回成功与否，并附带一个二维码）
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordPlusResp>
    updateAppScheduleRecord(RecordDto content,
                            CallBack<AppScheduleRecordPlusResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordPlusResp>(callBack) {
            @Override
            protected AppScheduleRecordPlusResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordOperateReq message = AppScheduleRecordOperateReq.newBuilder()
                        .setContent(content)
                        .build();
                Logger.d(message);
                AppScheduleRecordPlusResp response = null;
                try {
                    response = getStub(channel).updateAppScheduleRecord(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "updateAppScheduleRecord");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 交接班动作——接班确认（返回成功与否）
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AppScheduleRecordResp>
    checkAppScheduleRecord(RecordDto content,
                           CallBack<AppScheduleRecordResp> callBack) {
        return new GrpcAsyncTask<String, Void, AppScheduleRecordResp>(callBack) {
            @Override
            protected AppScheduleRecordResp doRequestData(ManagedChannel channel) {
                AppScheduleRecordOperateReq message = AppScheduleRecordOperateReq.newBuilder()
                        .setContent(content)
                        .build();
                Logger.d(message);
                AppScheduleRecordResp response = null;
                try {
                    response = getStub(channel).checkAppScheduleRecord(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "checkAppScheduleRecord");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    //生成交接班二维码

    private static class SingleTonHolder {
        private static final ScheduleAppApiService INSTANCE = new ScheduleAppApiService();
    }


}
